package com.mystore.service.impl;

import com.mystore.dao.CartDao;
import com.mystore.dao.impl.CartDaoImpl;
import com.mystore.entity.Cart;
import com.mystore.service.CartService;
import com.mystore.util.DBUtil;
import com.mystore.vo.GoodVO;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class CartServiceImpl implements CartService {

    /**
     * 查找用户的购物车
     * @param uid
     * @return
     */
    @Override
    public List<GoodVO> findMyCart(int uid) {
        Connection connection = DBUtil.getConntion();
        CartDao cartDao = new CartDaoImpl();
        List<GoodVO> list = cartDao.findAllMyCartByUid(uid,connection);
        DBUtil.closeConnection(connection);
        return list;
    }

    @Override
    public void updateCart(Cart cart) throws SQLException {
        Connection connection = DBUtil.getConntion();
        CartDao cartDao = new CartDaoImpl();
        Object object = cartDao.findByUidAndGid(cart.getUid(),cart.getGid(),connection);
        if(object==null){
              cartDao.save(cart,connection);
        }else{
             cartDao.updateCart(cart,connection);
        }
        DBUtil.closeConnection(connection);
    }

    /**
     * 添加到购物车
     * @param cart
     * @return
     */
    @Override
    public Serializable addCart(Cart cart) throws SQLException {
        Connection connection = DBUtil.getConntion();
        CartDao cartDao = new CartDaoImpl();
        Boolean save = cartDao.save(cart,connection);
        DBUtil.closeConnection(connection);
        return save;
    }
}
